﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
abstract class Shape
{
    public abstract bool IsValid { get; }
    public abstract double GetArea();
}

// 长方形
class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override bool IsValid => Length > 0 && Width > 0;

    public override double GetArea()
    {
        return IsValid ? Length * Width : 0;
    }
}

// 正方形
class Square : Shape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public override bool IsValid => Side > 0;

    public override double GetArea()
    {
        return IsValid ? Side * Side : 0;
    }
}

// 三角形
class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public override bool IsValid =>
        A > 0 && B > 0 && C > 0 && A + B > C && A + C > B && B + C > A;

    public override double GetArea()
    {
        if (!IsValid) return 0;

        double s = (A + B + C) / 2;
        return Math.Sqrt(s * (s - A) * (s - B) * (s - C)); // 海伦公式
    }
}
class work32{
    Shape rect = new Rectangle(5, 3);
    Shape square = new Square(4);
    Shape triangle = new Triangle(3, 4, 5);

    //PrintShapeInfo("长方形", rect);
    //PrintShapeInfo("正方形", square);
    //PrintShapeInfo("三角形", triangle);
    static void PrintShapeInfo(string name, Shape shape)
    {
        Console.WriteLine($"【{name}】是否合法: {shape.IsValid}");
        Console.WriteLine($"面积: {(shape.IsValid ? shape.GetArea().ToString("F2") : "无效")}\n");
    }
}
